<?php

namespace addons\aliyunsms\controller;

use app\common\controller\Backend;
use think\Exception;

/**
 * 阿里云短信管理
 */
class Index extends Backend
{
    protected $model = null;
    protected $noNeedLogin = ['test'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 测试短信发送
     */
    public function test()
    {
        $mobile = $this->request->post('mobile');
        $code = $this->request->post('code', '1234');
        
        if (!$mobile) {
            $this->error('请输入手机号');
        }
        
        if (!preg_match('/^\d+$/', $mobile)) {
            $this->error('手机号格式不正确');
        }
        
        try {
            $aliyunsms = new \addons\aliyunsms\library\Aliyunsms();
            $result = $aliyunsms->mobile($mobile)->msg("你的短信验证码是：{$code}")->send();
            
            if ($result) {
                $this->success('短信发送成功');
            } else {
                $this->error('短信发送失败：' . $aliyunsms->getError());
            }
        } catch (Exception $e) {
            $this->error('发送异常：' . $e->getMessage());
        }
    }
}
